import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Load_Window extends JFrame {

	private JPanel jContentPane = null;
	private Boolean error = false;
	
	public Load_Window(Window_Controller wc) {
		super();
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		initialize(wc);
	}
	
	/**
	 * Initialize the JFrame
	 * @param wc
	 */
	private void initialize(Window_Controller wc) {
		this.setContentPane(getJContentPane(wc));
		this.setTitle("Find din animation?");
		this.setSize(540,390);
		this.setResizable(false);
		this.setVisible(false);
	}
	
	/**
	 * Initialize the JPanel
	 * @param wc				(windowcontroller) Window_Controller
	 * @return
	 */
	private JPanel getJContentPane(Window_Controller wc) {
		
		final Window_Controller wcon = wc;
		final JFileChooser chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

		if (Main.srcPath.contains("/"))
			chooser.setCurrentDirectory(new File(Main.srcPath + "/saves"));
		else
			chooser.setCurrentDirectory(new File(Main.srcPath + "\\saves"));
			
		
		chooser.addActionListener(new ActionListener(){
			  public void actionPerformed(ActionEvent e){
				  if (e.getActionCommand() == "CancelSelection") {
					  dispose();
				  }
				  else if (e.getActionCommand() == "ApproveSelection") {
					  load_script(chooser.getSelectedFile().toString());
					  
					  try {
						  if (Main.srcPath.contains("/")) {
							  wcon.torso = ImageIO.read(new File(chooser.getSelectedFile().toString() + "/torso.jpg"));
							  wcon.head = ImageIO.read(new File(chooser.getSelectedFile().toString() + "/head.jpg"));
							  wcon.left_arm = ImageIO.read(new File(chooser.getSelectedFile().toString() + "/left_arm.jpg"));
							  wcon.right_arm = ImageIO.read(new File(chooser.getSelectedFile().toString() + "/right_arm.jpg"));
							  wcon.left_leg = ImageIO.read(new File(chooser.getSelectedFile().toString() + "/left_leg.jpg"));
							  wcon.right_leg = ImageIO.read(new File(chooser.getSelectedFile().toString() + "/right_leg.jpg"));
						  } else {
							  wcon.torso = ImageIO.read(new File(chooser.getSelectedFile().toString() + "\\torso.jpg"));
							  wcon.head = ImageIO.read(new File(chooser.getSelectedFile().toString() + "\\head.jpg"));
							  wcon.left_arm = ImageIO.read(new File(chooser.getSelectedFile().toString() + "\\left_arm.jpg"));
							  wcon.right_arm = ImageIO.read(new File(chooser.getSelectedFile().toString() + "\\right_arm.jpg"));
							  wcon.left_leg = ImageIO.read(new File(chooser.getSelectedFile().toString() + "\\left_leg.jpg"));
							  wcon.right_leg = ImageIO.read(new File(chooser.getSelectedFile().toString() + "\\right_leg.jpg"));
						  }
						  
						  ImageDB.images.put("krop", wcon.torso);
						  ImageDB.images.put("hben", wcon.right_leg);
						  ImageDB.images.put("vben", wcon.left_leg);
						  ImageDB.images.put("harm", wcon.right_arm);
						  ImageDB.images.put("varm", wcon.left_arm);
						  ImageDB.images.put("hoved", wcon.head);
					  } catch (Exception ex) {
						  error = true;
					  }
					  
					  if (error) {
						  wcon.show_Startup_Window();
						  setVisible(false);
					  }
					  else {
						  Main.default_bool = true;
						  wcon.show_WorldCreator("$krop = Mand.new(\"$krop\")");
						  setVisible(false);  
					  }
				  }
			  }
		});
		
		jContentPane = new JPanel();
		jContentPane.add(chooser);
		return jContentPane;
	}
	private void load_script(String load_name) {
		String script = ""; 
		try {
			BufferedReader in;
			if (Main.srcPath.contains("/"))
				in = new BufferedReader(new FileReader(load_name + "/common_script.rb"));
			else
				in = new BufferedReader(new FileReader(load_name + "\\common_script.rb"));
			
    		String str;
    		while ((str = in.readLine()) != null) {
    			script = script + str + "\n";
    		}
    		in.close();
    		
    		BufferedWriter out = new BufferedWriter(new FileWriter(Main.srcPath + "common_script.rb"));
    		out.write(script);
    		out.flush();
    		out.close();
		} catch (IOException io_ex) {
			error = true;
			System.out.println("IO Exception");
		}
		
		script = "";
		try {
			BufferedReader in;
			if (Main.srcPath.contains("/"))
				in = new BufferedReader(new FileReader(load_name + "/script.rb"));
			else
				in = new BufferedReader(new FileReader(load_name + "\\script.rb"));
			
			String str;
    		while ((str = in.readLine()) != null) {
    			script = script + str + "\n";
    		}
    		in.close();
    		
			BufferedWriter out = new BufferedWriter(new FileWriter(Main.srcPath + "script.rb"));
    		out.write(script);
    		out.flush();
    		out.close();
		} catch (IOException io_ex) {
			error = true;
			System.out.println("IO Exception");
		}
		
		return;
	}
}
